<?php
abstract class APF_Component {
    public function __construct($parent=NULL, $html_id=NULL) {
        $this->parent = $parent;
        $this->html_id = $html_id;
    }

    public function __destruct() {
    }

    /**
     * @return APF_Component
     */
    public function get_parent() {
        return $this->parent;
    }

    /**
     * @var APF_Component
     */
    private $parent;

    //

    /**
     * @return string
     */
    public function get_html_id() {
        return $this->html_id;
    }

    private $html_id;

    //

    public static function use_javascripts() {
        return array();
    }

    public static function use_styles() {
        return array();
    }

    public static function use_boundable_javascripts() {
        return array();
    }

    public static function use_boundable_styles() {
        return array();
    }

    public static function use_component() {
        return array();
    }

    //

    public function set_params($params=array()) {
        $this->params = $params;
    }

    public function get_params() {
        return $this->params;
    }

    public function get_param($name) {
        return $this->params[$name];
    }

    private $params;

    //

    public function assign_data($name, $value) {
        $this->data[$name] = $value;
    }

    public function get_data() {
        return $this->data;
    }

    private $data = array();

    //

    public function execute() {
        $view = $this->get_view();
        if ($view) {
            $f = apf_classname_to_path(get_class($this)).$view.'.phtml';
            $file =  "component/".$f;
            global $G_LOAD_PATH,$cached_files;
            if (defined('CACHE_PATH')) {
                $cf = apf_class_to_cache_file($f,"component");
                if (file_exists($cf)) {
                    $this->render($cf);
                    return;
                }
            }
            //if (apf_required_files($file,"component"));
            foreach ($G_LOAD_PATH as $path) {
                if (file_exists($path.$file)) {
                    $this->render($path.$file);
                    if (defined('CACHE_PATH')) {
                        apf_save_to_cache($f,"component",$path.$file);
                    }
                    break;
                }
            }
        }
    }

    public function script_block_begin() {
        ob_start();
    }

    public function script_block_end($order=0) {
        $content = ob_get_contents();
        APF::get_instance()->register_script_block($content,$order);
        ob_end_clean();
    }

    //

    abstract public function get_view();

    protected function render($file) {
        foreach ($this->data as $key=>$value) {
            $$key = $value;
        }
        $request = APF::get_instance()->get_request();
        $response = APF::get_instance()->get_response();
        include($file);
    }

    //

    /**
     * @return APF_Component
     */
    public function component($class, $params=array()) {
        return APF::get_instance()->component($this, $class, $params);
    }

    //

    /**
     * @return APF_Page
     */
    protected function get_page() {
        $object = $this->get_parent();
        while ($object) {
            if ($object instanceof APF_Page) {
                return $object;
            }
            $object = $object->get_parent();
        }
        return NULL;
    }
}
